package com.cd.servlet;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cd.dao.MovieCategoryDao;
import com.cd.entity.MovieCategory;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名：MovieCategoryServlet
 * 作者：WF
 * 功能：电影分类控制器
 */
@WebServlet("/movieCategory")
public class MovieCategoryServlet extends HttpServlet {
    private MovieCategoryDao movieCategoryDao;

    @Override
    public void init() throws ServletException {
        movieCategoryDao = new MovieCategoryDao();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String cmd = req.getParameter("cmd");
        if (StrUtil.isNotBlank(cmd)) {
            if ("list".equals(cmd)) {
                list(req, resp);
            }
            if ("add".equals(cmd)) {
                add(req, resp);
            }
            if ("update".equals(cmd)) {
                update(req, resp);
            }
            if ("delete".equals(cmd)) {
                delete(req, resp);
            }
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<MovieCategory> categories = movieCategoryDao.findAll();
        String json = JSONUtil.toJsonStr(categories);
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        MovieCategory category = objectMapper.readValue(req.getInputStream(), MovieCategory.class);
        Map<String, String> map = new HashMap<>();
        try {
            movieCategoryDao.add(category);
            map.put("code", "200");
            map.put("message", "添加电影分类成功！");
        } catch (IOException e) {
            map.put("code", "500");
            map.put("message", "添加电影分类失败！");
        }
        resp.getWriter().write(JSONUtil.toJsonStr(map));
        resp.getWriter().flush();
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        MovieCategory category = objectMapper.readValue(req.getInputStream(), MovieCategory.class);
        Map<String, String> map = new HashMap<>();
        try {
            movieCategoryDao.update(category);
            map.put("code", "200");
            map.put("message", "修改电影分类成功！");
        } catch (IOException e) {
            map.put("code", "500");
            map.put("message", "修改电影分类失败！");
        }
        resp.getWriter().write(JSONUtil.toJsonStr(map));
        resp.getWriter().flush();
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryIdStr = req.getParameter("categoryId");
        Integer categoryId = Integer.parseInt(categoryIdStr);
        Map<String, String> map = new HashMap<>();
        try {
            movieCategoryDao.delete(categoryId);
            map.put("code", "200");
            map.put("message", "删除电影分类成功！");
        } catch (IOException e) {
            map.put("code", "500");
            map.put("message", "删除电影分类失败！");
        }
        resp.getWriter().write(JSONUtil.toJsonStr(map));
        resp.getWriter().flush();
    }
}